package com.lemon.redis.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.json.JSONUtil;
import com.lemon.redis.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.concurrent.TimeUnit;

/**
 * @author 李猛
 * @datetime 2024/2/7 0:41
 * @description 类对象
 */
@Slf4j
@Service
public class RedisServiceImpl implements RedisService {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public boolean save(String key, Object value, long time, TimeUnit unit) {
        log.info("Redis缓存有时效的保存.save.key:{}", key);
        if (!StringUtils.hasText(key)) {
            return false;
        }
        try {
            redisTemplate.opsForValue().set(key, value, time, unit);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return false;
    }

    @Override
    public boolean save(String key, Object value, String time, TimeUnit unit) {
        return save(key, value, NumberUtil.parseLong(time), unit);
    }

    @Override
    public boolean save(String key, Object value) {
        log.info("Redis缓存保存.save.key:{}", key);
        if (!StringUtils.hasText(key)) {
            return false;
        }
        try {
            redisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return false;
    }

    @Override
    public boolean delByKey(String key) {
        log.info("Redis缓存根据key删除数据.delByKey.key:{}", key);
        if (!StringUtils.hasText(key)) {
            return false;
        }
        try {
            redisTemplate.delete(key);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return false;
    }

    @Override
    public <T> T getByKey(String key, Class<T> tClass) {
        log.info("Redis缓存根据key获取数据.getByKey.key:{}", key);
        if (!StringUtils.hasText(key)) {
            return null;
        }
        try {
            Object object = redisTemplate.opsForValue().get(key);
            return JSONUtil.toBean(JSONUtil.toJsonStr(object), tClass);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }

    @Override
    public boolean hasKey(String key) {
        log.info("Redis缓存是否存在key.hasKey.key:{}", key);
        if (!StringUtils.hasText(key)) {
            return false;
        }
        try {
            Boolean hasKey = redisTemplate.hasKey(key);
            return hasKey == null ? false : hasKey;
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return false;
    }

    @Override
    public boolean expireByKey(String key, long time, TimeUnit unit) {
        log.info("Redis缓存根据key设置过期时间.expireByKey.key:{}", key);
        if (!StringUtils.hasText(key)) {
            return false;
        }
        try {
            redisTemplate.expire(key, time, unit);
            return true;
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return false;
    }
}
